import re
import requests

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36version: 1.2.27'
}


def get_ip(request):
    # 判断是否使用代理
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        # 代理
        ip = x_forwarded_for.split(',')[0]
    else:
        # 获取真实ip
        ip = request.META.get('REMOTE_ADDR')
    return ip


def get_addr_info(ip):
    # if ip.startswith('10.') or ip.startswith('192') or ip.startswith('127'):
    #     return {'prov': '未知', 'city': '未知'}
    # url = f'https://www.ip138.com/iplookup.asp?ip={ip}&action=2'
    # res = requests.get(url=url, headers=headers).content.decode('gbk')
    # consequence = eval(re.findall(r'ip_result = (.*?);', res, re.S)[0])
    # addr = consequence['ip_c_list'][0]
    # addr.pop('begin')
    # addr.pop('end')
    # addr.pop('idc')
    # addr.pop('net')
    # if not addr.get('area'):
    #     addr.pop('area')
    # return addr
    return '{"prov":"河南","city":"测试"}'
